﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EITSurferInfinityLib;

namespace ControlerLib
{
    /// <summary>
    /// Interaction logic for ElectrodeContactControler.xaml
    /// </summary>
    public partial class ElectrodeContactControler : UserControl
    {
        List<Ellipse> Electrodes = new List<Ellipse>();
        ElectrodeErrorsDetection detector = new ElectrodeErrorsDetection();

        public ElectrodeContactControler()
        {
            InitializeComponent();
        }

        public void Create2DRoundPhantom(int NumberElectrode)
        {
            byte [] ElectrodeState = new byte[32];
            for(int i=0; i<NumberElectrode; i++)
            {
                ElectrodeState[i]=1;
            }
            Create2DRoundPhantom(NumberElectrode, ElectrodeState);
        }

        public void UpdateElectrodeState(double [] inData, int Offset)
        {
            detector.SetImpedanceData(inData);
            detector.SetOffset(Offset);
            detector.CalculateElectrodeState();
            Create2DRoundPhantom(inData.Length, detector.ElectrodeState);
        }
        

        public void Create2DRoundPhantom(int NumberElectrode, byte [] GoodElectrodeAreTrue)
        {
            // GoodElectrodeAreTrue = 0 no contact 
            // GoodElectrodeAreTrue = 1 medium contact 
            // GoodElectrodeAreTrue = 2 good contact
            // GoodElectrodeAreTrue = 3 neutral

            double mainradius = this.ActualHeight * 0.9 / 2.0;
            double margin = 0.05 * this.ActualHeight;
            Ellipse mainCircle = Circle(ref MainCaneva, mainradius);
            Ellipse lectrode1 = CircleAtAngle(ref MainCaneva, 10, 0, mainradius);

            MainCaneva.Children.Clear();
            Electrodes.Clear();


            MainCaneva.Children.Add(mainCircle);
            SetToMainCircle(mainCircle);

            for (int i = 0; i < NumberElectrode; i++)
            {
                double angle = (360.0 / NumberElectrode) * i;
                Ellipse TempElectrode = CircleAtAngle(ref MainCaneva, 5, angle, mainradius);

                switch (GoodElectrodeAreTrue[i])
                {
                    case 0:
                        SetToElectrodeBad(TempElectrode);
                        break;
                    case 1:
                        SetToElectrodeMedium(TempElectrode);
                        break;
                    case 2:
                        SetToElectrodeGood(TempElectrode);
                        break;
                    case 3:
                        SetToElectrodeNeutral(TempElectrode);
                        break;
                }

                Electrodes.Add(TempElectrode);
                MainCaneva.Children.Add(TempElectrode);
            }
        }

        public void SetToMainCircle(Ellipse circ)
        {
            circ.Fill = Brushes.White;
            circ.Stroke = Brushes.Black;
        }

        public void SetToElectrodeGood(Ellipse circ)
        {
            circ.Fill = Brushes.Green;
            circ.Stroke = Brushes.Black;
        }

        public void SetToElectrodeBad(Ellipse circ)
        {
            //mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
            circ.Fill = Brushes.Red;
            circ.Stroke = Brushes.Black;
        }

        public void SetToElectrodeNeutral(Ellipse circ)
        {
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            circ.Fill = Brushes.White;
            circ.Stroke = Brushes.Black;
        }

        public void SetToElectrodeMedium(Ellipse circ)
        {
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            circ.Fill = Brushes.Orange;
            circ.Stroke = Brushes.Black;
        }

        public Ellipse Circle(ref Canvas c, double Radius)
        {
            return Circle(ref c, Radius, 0, 0);
        }

        public Ellipse CircleAtAngle(ref Canvas c, double Radius, double Angle, double PositionRadius)
        {
            double grad = (Math.PI / 180.0) * Angle;
            double CenterX = Math.Sin(grad) * PositionRadius;
            double CenterY = Math.Cos(grad) * PositionRadius; ;

            return Circle(ref c, Radius, CenterX, CenterY);
        }

        public Ellipse Circle(ref Canvas c, double Radius, double CenterX, double CenterY)
        {
            double left = (c.ActualWidth / 2.0) - Radius-CenterX;
            double top= (c.ActualHeight / 2.0) - Radius-CenterY;
            double right = left;
            double bottom = top;
     

            Ellipse mainCircle = new Ellipse();
            mainCircle.Margin = new Thickness(left, top, right, bottom);
            mainCircle.Height = Radius*2.0;
            mainCircle.Width = Radius*2.0;
            

            return mainCircle;
        }

        private void MainCaneva_Loaded(object sender, RoutedEventArgs e)
        {
            Create2DRoundPhantom(32);
        }
    }
}
